package algorithm.leetcode;

import algorithm.leetcode.codeutils.ListNode;

/**
 * @program: util
 * @description: 删除链表倒数第N个节点
 * @author: zhouliangliang
 * @create: 2021-04-12 17:27
 **/
public class DeleteNThReciprocalNode {

    /**
     * i,j两个指针均指向head，j先走n步，i在接着走，i指向的就是倒数第n个节点
     * @param node
     * @param n
     */
    public void delete(ListNode<Integer> node, int n) {

        ListNode<Integer> head = new ListNode<>(-1, node);
        ListNode<Integer> tmp = head;
        ListNode<Integer> output = node;
        //move n steps
        while (head != null && n !=0) {
            n--;
            head = head.getNext();
        }


        while (head.getNext() != null) {
            head = head.getNext();
            tmp = tmp.getNext();
        }

        tmp.setNext(tmp.getNext().getNext());
        output.toString();
    }

    public static void main(String[] args) {
        ListNode<Integer> nodes = new ListNode<>(1, new ListNode<>(2, new ListNode<>(3, new ListNode<>(4, new ListNode<>(5, null)))));
        nodes.toString();
        new DeleteNThReciprocalNode().delete(nodes, 1);
    }
}
